package com.clip.dao;

import java.math.BigInteger;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.clip.entity.ClipEvent;
@Repository
public class ClipEventDao extends BasicDao{

    private static final Log log = LogFactory.getLog(ClipEventDao.class);
	//property constants
	public static final String UID = "uid";
	public static final String TITLE = "title";
	public static final String ADDRESS = "address";
	public static final String CONTENT = "content";
	public static final String BEGINTIME = "begintime";
	public static final String ENDTIME = "endtime";
	public static final String ALERTTIME = "alerttime";
	public static final String EVENTTYPE = "eventtype";
	public static final String CLIPTYPE = "cliptype";
	public static final String SHAREID="shareid";
    public void save(ClipEvent transientInstance) {
        log.debug("saving ClipEvent instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(ClipEvent persistentInstance) {
        log.debug("deleting ClipEvent instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public ClipEvent findById( java.lang.Integer id) {
        log.debug("getting ClipEvent instance with id: " + id);
        try {
        	ClipEvent instance = (ClipEvent) getSession()
                    .get(ClipEvent.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(ClipEvent instance) {
        log.debug("finding ClipEvent instance by example");
        try {
            List results = getSession()
                    .createCriteria("ClipEvent")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding ClipEvent instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from ClipEvent as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByUid(Object uid
	) {
		return findByProperty(UID, uid
		);
	}
	
	public List findByTitle(Object title
	) {
		return findByProperty(TITLE, title
		);
	}
	
	public List findByAddress(Object address
	) {
		return findByProperty(ADDRESS, address
		);
	}
	
	public List findByContent(Object content
	) {
		return findByProperty(CONTENT, content
		);
	}
	
	public List findByBegintime(Object begintime
	) {
		return findByProperty(BEGINTIME, begintime
		);
	}
	
	public List findByEndtime(Object endtime
	) {
		return findByProperty(ENDTIME, endtime
		);
	}
	
	public List findByAlerttime(Object alerttime
	) {
		return findByProperty(ALERTTIME, alerttime
		);
	}
	

	public List findAll() {
		log.debug("finding all ClipEvent instances");
		try {
			String queryString = "from ClipEvent";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public ClipEvent merge(ClipEvent detachedInstance) {
        log.debug("merging DateEvent instance");
        try {
        	ClipEvent result = (ClipEvent) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(ClipEvent instance) {
        log.debug("attaching dirty ClipEvent instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(ClipEvent instance) {
        log.debug("attaching clean ClipEvent instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public List findByQuery(String query) {
		return findByQuery(query,null);
	}
	
	public List findByQuery(String queryStr,Object...args ){
		Query query =getSession().createQuery(queryStr);
		if(args!=null&&args.length>0){
			for (int i = 0; i < args.length; i++) {
				query.setParameter(i, args[i]);
			}
		}
		return query.list();
	}
	
	public Object getUniquerResult(String query){
		return getSession().createQuery(query).uniqueResult();
	}
	
	public int countByQuery(String query){
		return ((Long)getSession().createQuery(query).iterate().next()).intValue();
	}
	
	public int countByQuery(String query,Object...args){
		Query queryobj=getSession().createQuery(query);
		if(args!=null&&args.length>0){
			for (int i = 0; i < args.length; i++) {
				queryobj.setParameter(i, args[i]);
			}
		}
		return ((Long)queryobj.iterate().next()).intValue();
	}
	
	public List findBySqlQuery(String query) {
		return getSession().createSQLQuery(query).list();
	}
	
	public void updateByQuery(String query) {
		getSession().createQuery(query).executeUpdate();
	}

	public List pagingByQuery(String queryStr, Integer currentpage,
			Integer pagesize) {
		Query query =getSession().createQuery(queryStr);
		query.setMaxResults(pagesize);
		query.setFirstResult(currentpage*pagesize);
		return query.list();
	}
	
	public List pagingByQuery(String queryStr, Integer currentpage,Integer pagesize,Object...args) {
		Query query =getSession().createQuery(queryStr);
		query.setMaxResults(pagesize);
		query.setFirstResult(currentpage*pagesize);
		if(args!=null&&args.length>0){
			for (int i = 0; i < args.length; i++) {
				query.setParameter(i, args[i]);
			}
		}
		return query.list();
	}
	
	public List pagingBySqlQuery(String queryStr, Integer currentpage,
			Integer pagesize) {
		SQLQuery query =getSession().createSQLQuery(queryStr);
		query.setMaxResults(pagesize);
		query.setFirstResult(currentpage*pagesize);
		return query.list();
	}

	public List findByNamedQuery(String namedquery, Object[] objects) {
		Query query=getSession().getNamedQuery(namedquery);
		int index=0;
		for (Object object : objects) {
			if(object instanceof String){
				query.setString(index, (String)object);
			}else if(object instanceof Integer){
				query.setInteger(index, (Integer)object);
			}else if(object instanceof Long){
				query.setLong(index, (Long)object);
			}
			index++;
		}
		return query.list();
	}
	
	public Long countByNamedQuery(String namedquery, Object[] objects) {
		Query query=getSession().getNamedQuery(namedquery);
		int index=0;
		for (Object object : objects) {
			if(object instanceof String){
				query.setString(index, (String)object);
			}else if(object instanceof Integer){
				query.setInteger(index, (Integer)object);
			}else if(object instanceof Long){
				query.setLong(index, (Long)object);
			}
			index++;
		}
		BigInteger integer=(BigInteger) query.uniqueResult();
		return integer.longValue() ;
	}
	
	public Object uniqueResultByNamedQuery(String namedquery, Object[] objects) {
		Query query=getSession().getNamedQuery(namedquery);
		int index=0;
		for (Object object : objects) {
			if(object instanceof String){
				query.setString(index, (String)object);
			}else if(object instanceof Integer){
				query.setInteger(index, (Integer)object);
			}else if(object instanceof Long){
				query.setLong(index, (Long)object);
			}
			index++;
		}
		return query.uniqueResult();
	}
}
